#自定义中间件类(执行登录判断)
from django.shortcuts import redirect
from django.urls import reverse
import re
class ShopMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        print('ShopMiddleware')
        # One-time configuration and initialization.

    def __call__(self, request):
        path = request.path
        print("url:",path)
        #判断管理后台是否登录
        #定义后台可以允许直接访问的url列表
        urllist = ['/myadmin/login','/myadmin/logout','/myadmin/dologin','/myadmin/verify']
        #判断当前请求url地址是否是以myadmin开头,并且部在urlist中,才做登录判断
        # Code to be executed for each request before
        # the view (and later middleware) are called.
        #if re.match(r'^/myadmin',path) and (path not in urllist):
            #判断是否登录
            #if 'adminuser' not in request.session:
            #重定向到登录页
                #return redirect(reverse('myadmin_login'))

        
        #if re.match(r'^/web',path):
            #if 'webuser' not in request.session:
                #return redirect(reverse('web_login'))

        urllist = ['/mobile/register','/mobile/doregister']
        #判断当前请求url地址是否是以myadmin开头,并且部在urlist中,才做登录判断
        # Code to be executed for each request before
        # the view (and later middleware) are called.
        if re.match(r'^/mobile',path) and (path not in urllist):
            #pass
            #判断是否登录
            if 'mobileuser' not in request.session:
            #重定向到登录页
                return redirect(reverse('mobile_register'))



        response = self.get_response(request)

        # Code to be executed for each request/response after
        # the view is called.

        return response